﻿using SIIE_ERP.Codigo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIIE_ERP.BaseDatos.Configuracion 
{
    public class dbo_Sec_MenuUser  
    {
        #region Propiedades

        #endregion
        
        #region Constructores

        public dbo_Sec_MenuUser()
        {
        }

        #endregion
        
        #region INSERT

        public Respuesta Insert_Sec_MenuUser(
     
	        int IdMenu,
            string RFC,
	        int Id_Usuario,	        
	        bool IsActive
        )
        {
            try
            {
                string Query = @"INSERT INTO dbo_Sec_MenuUser
                             (
	                            IdMenu,
                                RFC,
	                            Id_Usuario,	        
	                            IsActive
                             )
                             VALUES
                             (
                                @@IdMenu,
                                '@@RFC',
	                            @@Id_Usuario,	        
	                            @@IsActive
                             )";

                Query = Query.Replace("@@IdMenu", IdMenu.ToString());
                Query = Query.Replace("@@RFC", RFC);
                Query = Query.Replace("@@Id_Usuario", Id_Usuario.ToString());
                
                if(IsActive)
                    Query = Query.Replace("@@IsActive", "1");
                else
                    Query = Query.Replace("@@IsActive", "0");                
                
                return SrvDB.ExecuteQuery_Insert(Query);
            }
            catch (Exception Ex)
            {
                return new Respuesta(2, Ex.Message);
            }
        }


        #endregion

        #region UPDATE

        public Respuesta Update_MenuUser(
            
            int IdMenu,
            string RFC,
            int Id_Usuario,
            bool IsActive          
        )
        {
            try
            {
                DataTable Aux = Get_Menus_by_RFC(Id_Usuario, RFC);
                bool Existe = false;
                for (int i = 0; i < Aux.Rows.Count; i++)
                {
                    if (Int32.Parse(Aux.Rows[i]["IdMenu"].ToString()) == IdMenu)
                        Existe = true;
                }

                if (Existe)
                {

                    string Query = @"UPDATE dbo_Sec_MenuUser SET
                                                                 
                                    IsActive = @@IsActive                                  
                                    
                                 WHERE 
                                    IdMenu = @@IdMenu AND 
                                    RFC = '@@RFC' AND
                                    Id_Usuario = @@Id_Usuario";

                    Query = Query.Replace("@@IdMenu", IdMenu.ToString());
                    Query = Query.Replace("@@RFC", RFC);
                    Query = Query.Replace("@@Id_Usuario", Id_Usuario.ToString());

                    if (IsActive)
                        Query = Query.Replace("@@IsActive", "1");
                    else
                        Query = Query.Replace("@@IsActive", "0");

                    return SrvDB.ExecuteQuery_Update(Query);
                }
                else
                {
                    return Insert_Sec_MenuUser(IdMenu, RFC, Id_Usuario, IsActive);
                }
            }
            catch (Exception Ex)
            {
                return new Respuesta(2, Ex.Message);
            }
        }        

        #endregion

        #region SELECT

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Nombre">Nombre de Menu_User que se ingresa en el Login.</param>
        /// <returns>Devuelve -1 en caso de ocurrir un error, cualquier otro número corresponde al Id del Menu_User.</returns>
        public DataTable Get_Id_MenuUser(string RFC, int Id_Usuario)
        {
            string Query = "SELECT * FROM dbo_Sec_MenuUser WHERE RFC = '@@RFC' AND Id_Usuario = @@Id_Usuario";
            Query = Query.Replace("@@RFC", RFC);
            Query = Query.Replace("@@Id_Usuario", Id_Usuario.ToString());

            return SrvDB.ExecuteQuery_Get(Query);            
        }

        public DataTable Get_RFCs_by_Id(int Id_Usuario)
        {
            string Query = "SELECT DISTINCT RFC FROM dbo_Sec_MenuUser WHERE Id_Usuario = @@Id_Usuario";
            Query = Query.Replace("@@Id_Usuario", Id_Usuario.ToString());

            return SrvDB.ExecuteQuery_Get(Query);
        }

        public DataTable Get_Menus_by_RFC(int Id_Usuario, string RFC )
        {
            string Query = "SELECT * FROM dbo_Sec_MenuUser WHERE Id_Usuario = @@Id_Usuario AND RFC = '@@RFC'";
            Query = Query.Replace("@@Id_Usuario", Id_Usuario.ToString());
            Query = Query.Replace("@@RFC", RFC);

            return SrvDB.ExecuteQuery_Get(Query);
        }


        #endregion
    }
}
